package ext.tianma.project2.APQP.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import wt.org.WTUser;
import wt.session.SessionHelper;
import wt.session.SessionServerHelper;
import wt.util.WTException;
import ext.tianma.project2.APQP.constants.APQPProjectConstant;
import ext.tianma.project2.APQP.service.HomeService;
import ext.tianma.util.TMUtil;

/**
 * 类名: HomeController </br>
 * 包名: ext.tianma.project2.APQP.controller
 * 描述: APQP我的待办
 * @author xucr
 * 创建时间: 2020年8月17日
 * 发布版本: V1.0
 */

@Controller
@RequestMapping({ "/HomeController" })
public class HomeController {

	@Autowired
	HomeService homeservice;
	
	private static final org.slf4j.Logger logger = LoggerFactory.getLogger(HomeController.class);
	/**
	 * 待我处理的项目任务
	 * @param params
	 * @param request
	 * @param response
	 * @throws IOException
	 * @author xucr
	 * @CreateDate 2020年8月17日
	 */
	@RequestMapping(value = "/getMyTasks", method = {RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void getMyTasks(
			@RequestParam(value = "params", required = false) String params,
			HttpServletRequest request, HttpServletResponse response) throws IOException  {
		/**
		 * 待处理：项目进行中，且状态为进行中、不达标、“我”的活动（PS：未上载的一定是不达标，已上载不达标的，是流程驳回需要重新上载或者修订的）
		 * 
		 * 已完成：
		 * 	①所有状态项目，且状态为已完成、“我”的活动（一定是已达标的）
		 * 	②所有状态项目，且状态为进行中、已达标、“我”的活动
		 * 等价于：所有状态、已达标、我的活动
		 * */
		logger.info("getMyTasks >>>> params：" + params);
		boolean enforce = SessionServerHelper.manager.setAccessEnforced(false);
		response.setContentType("text/html;charset=UTF-8");
		TMUtil.setHeader(response);
		PrintWriter out = response.getWriter();
		try {
			if(StringUtils.isNotEmpty(params)) {
				JSONObject obj = new JSONObject(params);
				String type = obj.getString("type");
				Integer page = obj.getInt("page");
				Integer limit = obj.getInt("limit");
				WTUser user = (WTUser) SessionHelper.manager.getPrincipal();
				JSONObject result = null;
				if("1".equals(type)) { //待处理
					result = homeservice.getMyToDoTask(user, page, limit);
				} else if("2".equals(type)) {//已完成
					result = homeservice.getMyCompletedTasks(user, page, limit);
				}
				out.print(result.toString());
			}
		} catch (WTException e) {
			e.printStackTrace();
			logger.error("getMyToDoTask >>>catch1 >>>>>: " + e.getLocalizedMessage(Locale.getDefault()));
		} catch (JSONException e) {
			e.printStackTrace();
			logger.error("getMyToDoTask >>>catch2 >>>>>: " + e.getMessage());
		} finally {
			SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	
	/**
	 * 待我处理的流程任务
	 * @param params
	 * @param request
	 * @param response
	 * @throws IOException
	 * @author xucr
	 * @CreateDate 2020年8月17日
	 */
	@RequestMapping(value = "/getMyWorkItems", method = {RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void getMyWorkItems(
			@RequestParam(value = "params", required = false) String params,
			HttpServletRequest request, HttpServletResponse response) throws IOException  {
		/**
		 * 待处理：待我审批的流程
		 * 已完成：我已完成的流程
		 * 我发起：不考虑，基于APQP，目前没有自主发起流程的动作，均由系统自动发起
		 * */
		logger.info("getMyWorkItems >>>> params：" + params);
		boolean enforce = SessionServerHelper.manager.setAccessEnforced(false);
		response.setContentType("text/html;charset=UTF-8");
		TMUtil.setHeader(response);
		PrintWriter out = response.getWriter();
		try {
			WTUser user = (WTUser) SessionHelper.manager.getPrincipal();
			if(StringUtils.isNotEmpty(params)) {
				JSONObject obj = new JSONObject(params);
				String type = obj.getString("type");
				Integer page = obj.getInt("page");
				Integer limit = obj.getInt("limit");
				
				String[] workItemStates = new String [1];
				if("1".equals(type)) { //待处理
					workItemStates [0] = APQPProjectConstant.WORKITEM_STATUS_POTENTIAL;
				} else if("2".equals(type)) {//已完成
					workItemStates [0] = APQPProjectConstant.WORKITEM_STATUS_COMPLETED;
				}
				JSONObject result = homeservice.getMyWorkItems(user, workItemStates, page, limit);
				out.print(result.toString());
			}
		} catch (WTException e) {
			e.printStackTrace();
			logger.error("getMyToDoWorkItems >>>catch1 >>>>>: " + e.getLocalizedMessage(Locale.getDefault()));
		} catch (JSONException e) {
			e.printStackTrace();
			logger.error("getMyToDoWorkItems >>>catch2 >>>>>: " + e.getMessage());
		} finally {
			SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
}
